function createtr(){
	var tr=document.createElement("tr");
	for(var i=0;i<14;i++){
		var td1=document.createElement("td");
		tr.appendChild(td1);
	}
	 return tr;
	
}
 var table=document.querySelector("#table");
 for(var i=0;i<14;i++){
		 var tr=createtr();
		 table.appendChild(tr);
	 
 }
 /*创建代表棋盘的二位数组*/
 var arrs =[];
 for(var j=0;j<15;j++){
	  var arr = [];
	  for(var k=0;k<15;k++){
		  arr[k]=0;
	  }
	  arrs.push(arr);
 }
 console.log(arrs);
 /*注册点击事件（下棋）*/
 
 var wrapper=document.querySelector(".wrapper");
 function getPos(pos){
	 var x=pos%40;
	 if(x>20){
		 return pos - x + 40;
	 }else{
		 return pos - x;
	 }
	 
 }
 //判断垂直方向
 function judge1(y,x){
	 var value=arrs[y][x];
	 var count=1,c=y;
	 while(true){
		 c--;
		 if(!arrs[c]||arrs[c][x] !=value){
			 break;
		 }
		 count++;
	 }
	 c=y;
	 while(true){
		 c++;
		 if(!arrs[c]||arrs[c][x] !=value){
			 break;
		 }
		 count++;
	 }
	 return count;
 }
 //水平方向
 function judge2(y,x){
	var value = arrs[y][x];
	var count=1,c=x;
	while(true){		
		c--;
		if(!arrs[y][c] || arrs[y][c] != value){
			break;
		}
		count++;
	}

	c=x;
	while(true){
		c++;
		if(!arrs[y][c] || arrs[y][c] != value){
			break;
		}
		count++;
	}
	
	return count;
}
//判断斜向的
function judge3(y,x){
	var value = arrs[y][x];
	var count=1,c=x,d=y;
	while(true){
		c--;
		d--;
		if(!arrs[d] || !arrs[d][c] || arrs[d][c] !=value){
			break;
		}
		count++;
	}
	
	c=x,d=y;
	while(true){
		c++;
		d++;
		if(!arrs[d][c]||!arrs[d][c]||arrs[d][c] !=value){
			break;
		}
		count++;
	}
	return count;
}

 //左斜线
 function judge4(y,x){
	var value = arrs[y][x];
	var count=1,c=x,d=y;
	while(true){
		c--;
		d++;
		if(!arrs[d] || !arrs[d][c] || arrs[d][c] !=value){
			break;
		}
		count++;
	}
	
	c=x,d=y;
	while(true){
		c++;
		d--;
		if(!arrs[d][c]||!arrs[d][c]||arrs[d][c] !=value){
			break;
		}
		count++;
	}
	return count;
}

 var curqizi="hei";
 var qizizs = document.querySelector("#qizi-cur");
 wrapper.onclick=function(event){
	 if(curqizi=="success"){
		 alert("对局结束");
		 return;
	 }
	 var offsetLeft=wrapper.offsetLeft;
	 var offsetTop=wrapper.offsetTop;

	 var dx=event.pageX-offsetLeft;
	 var dy=event.pageY-offsetTop;
	 
	 
	 var qz=document.createElement("img");
	 qz.setAttribute("src",curqizi+".png");
	 qz.className="qizi";
	 dy=getPos(dy);
	 dx=getPos(dx);
	 //判断是否重复落子
	 if(arrs[dy/40][dx/40]){
		 alert("不能重复落子！");
		 return;
	 }
	 //判断当前落子的颜色，修改二维数组
	 if(curqizi=="hei"){
		 arrs[dy/40][dx/40]=2;
	 }else{
		 arrs[dy/40][dx/40]=1;
	 }

	 qz.style.top=(dy-18)+"px";
	 qz.style.left=(dx-18)+"px";
	 wrapper.appendChild(qz);
	 //判断棋子有没有5个连在一起
	 var count1=judge1(dy/40,dx/40);
	  var count2=judge2(dy/40,dx/40);
	    var count3=judge3(dy/40,dx/40);
		var count4=judge4(dy/40,dx/40);
	 if(count1>=5||count2>=5||count3>=5||count4>=5){
		   if(curqizi=="hei"){
		 alert("黑棋胜");
		 }else{
		alert("白棋胜");
		 }
		 curqizi="success";
		 return;
	 }

	
	 //交换黑白棋
	 if(curqizi=="hei"){
		 
		 curqizi="bai";
		 }else{
			 curqizi="hei";
		 }
		qizizs.src=curqizi+".png";
	 
 }